package com.example.marts.mobilelab3;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.ImageView;

public class MainActivityTest extends AppCompatActivity implements SensorEventListener{

    //Sensor
    private SensorManager sensorManager;
    private Sensor gravitySensor;

    //The ball to move around on the screen
    private ImageView ball;

    //The width of the frame
    private int frameWidth;
    //The height of the frame
    private int frameHeight;
    //Used to make the ball stay within the frame
    private int radius;

    //Used to play a sound when the ball hits the edge of the frame
    private ToneGenerator toneGenerator;

    //Gets the screens height and width
    DisplayMetrics displayMetrics;
    private int devHeight;
    private int devWidth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Gets the frame and the ball
        ImageView frame = findViewById(R.id.frame);
        ball = findViewById(R.id.ball);

        //Initiates a new ToneGenerator
        toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, ToneGenerator.MAX_VOLUME);

        //Sets the height and with of the frame
        frameWidth = frame.getWidth();
        frameHeight = frame.getHeight();

        //Sets the radius
        radius = 25;

        //Gets the system sensors
        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        assert sensorManager != null;
        //Selects the gravitySensor sensor
        gravitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);

        //Gets the screens height and width
        displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        devHeight = displayMetrics.heightPixels;
        devWidth = displayMetrics.widthPixels;

        //Creates a listener on the sensor
        sensorManager.registerListener(this, gravitySensor, SensorManager.SENSOR_DELAY_GAME);
        //Set the delay to GAME as that created a less "laggy" experience

    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        float Y = ball.getY();

        //The coordinates the ball moves to
        float nextY = Y + sensorEvent.values[1] * 2;

        float valueSensorEvent = sensorEvent.values[1];
        if((nextY - radius) >= (frameHeight / 2) + 25){
            ball.setY(nextY);
        } else{
            onFrameHit();
            ball.setY(nextY + 35);
        }
        if ((nextY + radius) > devHeight - (frameHeight / 2) - 320) {
            onFrameHit();
            ball.setY(nextY - 35);
        }

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {
        //NOT USED, but implemented as SensorEventListener needs it
    }

   @Override
    protected void onResume() {
        super.onResume();
       //Creates a listener on the gravity-sensor on resuming the app
       sensorManager.registerListener(this, gravitySensor, SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //Removes the listener when the app is not in use
        sensorManager.unregisterListener(this);
    }

    /**
     * Sets of multiple effects when the ball hits the edge of the frame
     */
    private void onFrameHit(){
        //Vibrator
//        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
//        assert vibrator != null;
//        vibrator.vibrate(400);

        //Sound
//        toneGenerator.startTone(ToneGenerator.TONE_CDMA_ONE_MIN_BEEP);
    }
}
